package com.subang.function.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbEvaluation;
import com.subang.function.service.ISbEvaluationService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 用户评价Controller
 * 
 * @author subei
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/function/evaluation")
public class SbEvaluationController extends BaseController
{
    @Autowired
    private ISbEvaluationService sbEvaluationService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户评价列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SbEvaluation sbEvaluation)
    {
        startPage();
        List<SbEvaluation> list = sbEvaluationService.selectSbEvaluationList(sbEvaluation);
        return getDataTable(list);
    }

    /**
     * 导出用户评价列表
     */
    @PreAuthorize("@ss.hasPermi('function:evaluation:export')")
    @Log(title = "用户评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbEvaluation sbEvaluation)
    {
        List<SbEvaluation> list = sbEvaluationService.selectSbEvaluationList(sbEvaluation);
        ExcelUtil<SbEvaluation> util = new ExcelUtil<SbEvaluation>(SbEvaluation.class);
        util.exportExcel(response, list, "用户评价数据");
    }

    /**
     * 获取用户评价详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sbEvaluationService.selectSbEvaluationById(id));
    }

    /**
     * 新增用户评价
     */

    @Log(title = "用户评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbEvaluation sbEvaluation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbEvaluation.setUserId(String.valueOf(user.getUserId()));
        sbEvaluation.setUserName(user.getNickName());
        sbEvaluation.setEvaluationDate(new Date());
        return toAjax(sbEvaluationService.insertSbEvaluation(sbEvaluation));
    }

    /**
     * 修改用户评价
     */

    @Log(title = "用户评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbEvaluation sbEvaluation)
    {
        return toAjax(sbEvaluationService.updateSbEvaluation(sbEvaluation));
    }

    /**
     * 删除用户评价
     */

    @Log(title = "用户评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sbEvaluationService.deleteSbEvaluationByIds(ids));
    }


    @RequestMapping("/evaluationCheck")
    public AjaxResult evaluationCheck(@RequestBody RequestData requestData) {

            SysUser user = userService.getUserByOpenId(SecurityUtils.getLoginUser().getUser().getOpenid());

            String teacherId = requestData.getTeacherId();
            String subject = requestData.getSubject();
            Map<String, Object> params = new HashMap<>();
            params.put("teacherId", teacherId);
            params.put("subject", subject);
            params.put("userId", user.getUserId());
            return success(sbEvaluationService.selectSbEvaluationByTeacherIdAndSubject(params));

    }

    public static class RequestData {
        private String teacherId;
        private String subject;

        public String getTeacherId() {
            return teacherId;
        }

        public void setTeacherId(String teacherId) {
            this.teacherId = teacherId;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }
    }
}
